<?php
//设置编码格式
header('content-type:text/html;charset=utf-8');
define('IN_TG', 'admin_action');
require ('includes/common.inc.php');
//设置返回数组初始状态
$return_data=array();
$return_data['status']=0;
$return_data['msg']='';
$return_data['item']=array();
//开启session
session_start();
//判断是否接收到type
if(isset($_POST['type']))
{
    //用户图表
    if($_POST['type']=='user_chart')
    {
        $sql = "select count(*) as visits,date_format(tg_reg_time,'%Y-%m-%d') as d from tg_user group by date_format(tg_reg_time,'%Y-%m-%d') order by date_format(tg_reg_time,'%Y-%m-%d') desc";
        $res = _query($sql);
        $data=array();
        while($row = _fetch_array_list($res))
        {
            $data[]=$row;
        }
        //查找记录起始时间
        $startData=_fetch_array("select date_format(tg_reg_time,'%M %d,%Y') as d from tg_user order by tg_reg_time limit 1");
        //查找记录最终时间
        $endtData=_fetch_array("select date_format(tg_reg_time,'%M %d,%Y') as d from tg_user order by tg_reg_time desc limit 1");
        //判断结果集是否大于0
        if(count($data)>0)
        {
            $return_data['status']=1;
            $return_data['item']['list']=$data;
        }
        else
        {
            $return_data['status']=0;
            $return_data['msg']='暂无用户数据';
        }
        $return_data['item']['start_time']=$startData['d'];
        $return_data['item']['end_time']=$endtData['d'];
    } //帖子图表
    else if($_POST['type']=='artice_chart')
    {
        //生成sql语句
        $sql = "select count(*) as `value`,concat('类型',tg_type) as label from tg_article group by tg_type";
        //执行sql语句
        $res = _query($sql);
        $data=array();
        //循环获取每一个元素
        while($row = _fetch_array_list($res))
        {
            $data[]=$row;
        }
        //查找记录起始时间
        $startData=_fetch_array("select date_format(tg_date,'%M %d,%Y') as d from tg_article order by tg_date limit 1");
        //查找记录最终时间
        $endtData=_fetch_array("select date_format(tg_date,'%M %d,%Y') as d from tg_article order by tg_date desc limit 1");
        //判断结果集是否大于0
        if(count($data)>0)
        {
            $return_data['status']=1;
            $return_data['item']['list']=$data;
        }
        else
        {
            $return_data['status']=0;
            $return_data['msg']='暂无帖子数据';
        }
        $return_data['item']['start_time']=$startData['d'];
        $return_data['item']['end_time']=$endtData['d'];
    } //后台登录
    else if($_POST['type']=='admin_go_login')
    {
        //获取前台传递的值，并进行过滤
        $admin_user=_mysql_string($_POST['admin_user']);
        $admin_pwd=_mysql_string($_POST['admin_pwd']);
        //判断用户名和密码
        if($admin_user=='root'&&$admin_pwd=='root')
        {
            $return_data['status']=1;
            $_SESSION['admin_sess']=md5('root').time();
        }
        else
        {
            $return_data['status']=0;
            $return_data['msg']='用户名或者密码不正确';
        }
    } //用户启用修改
    else if($_POST['type']=='user_active_change')
    {
        //获取前台传递的值，并进行过滤
        $user_id=_mysql_string($_POST['user_id']);
        $user_active=_mysql_string($_POST['user_active']);
        //执行sql语句
        $res=_query("update tg_user set tg_active={$user_active} where tg_id={$user_id}");
        //判断影响行数
        if(_affected_rows()>0)
        {
            $return_data['status']=1;
        }
        else
        {
            $return_data['status']=0;
            $return_data['msg']='请不要重复操作';
        }
    } //相册启用修改
    else if($_POST['type']=='photo_active_change')
    {
        //获取前台传递的值
        $photo_id=_mysql_string($_POST['photo_id']);
        $photo_active=_mysql_string($_POST['photo_active']);
        //执行sql语句
        $res=_query("update tg_photo set tg_active={$photo_active} where tg_id={$photo_id}");
        //判断影响行数
        if(_affected_rows()>0)
        {
            $return_data['status']=1;
        }
        else
        {
            $return_data['status']=0;
            $return_data['msg']='请不要重复操作';
        }
    } //系统信息修改
    else if($_POST['type']=='system_info_change')
    {
        //获取前台传递的值，并进行过滤
        $webname=_mysql_string($_POST['webname']);
        $article_num=_mysql_string($_POST['article_num']);
        $blog_num=_mysql_string($_POST['blog_num']);
        $photo_num=_mysql_string($_POST['photo_num']);
        $skin_num=_mysql_string($_POST['skin_num']);
        $post_num=_mysql_string($_POST['post_num']);
        $reply_num=_mysql_string($_POST['reply_num']);
        //生成sql语句
        $sql="update tg_system set tg_webname='{$webname}',tg_article={$article_num},tg_blog={$blog_num},tg_photo={$photo_num},tg_skin={$skin_num},tg_post={$post_num},tg_re={$reply_num}";
        //执行sql语句
        $res=_query($sql);
        //判断影响行数
        if(_affected_rows()>0)
        {
            $return_data['status']=1;
            $return_data['msg']='修改成功';
        }
        else
        {
            $return_data['status']=0;
            $return_data['msg']='请不要重复操作';
        }
    }
    //修改敏感词
    else if($_POST['type']=='system_word_change')
    {
        //获取前台传递的值，并进行过滤
        $word_item=_mysql_string($_POST['word_item']);
        $word_old_item=_mysql_string($_POST['word_item_old']);
        //生成sql语句，查找敏感词内容
        $sql1="select tg_string from tg_system limit 1";
        //执行sql语句
        $res = _fetch_array($sql1);
        //字符串按照 | 切割成数组
        $string_arr=explode('|',$res['tg_string']);
        $string_len=count($string_arr);
        //循环判断是否存在修改的值，存在则修改
        for($i=0;$i<$string_len;$i++)
        {
            if($string_arr[$i]==$word_old_item)
            {
                $string_arr[$i]=$word_item;
                break;
            }
        }
        //数组 按照 | 生成字符串
        $tg_string=implode('|',$string_arr);
        //修改敏感词字段的值
        $row=_query("update tg_system set tg_string='{$tg_string}'");
        //判断影响行数
        if(_affected_rows()>0)
        {
            $return_data['status']=1;
            $return_data['msg']='修改成功';
        }
        else
        {
            $return_data['status']=0;
            $return_data['msg']='请不要重复操作';
        }
    }
    //删除敏感词
    else if ($_POST['type'] == 'system_word_delete')
    {
        //获取前台传递的值，并进行过滤
        $word_item = $_POST['word_item'];
        //生成sql语句，查找敏感词内容
        $sql1="select tg_string from tg_system limit 1";
        //执行sql语句
        $res = _fetch_array($sql1);
        $string_arr=explode('|',$res['tg_string']);
        $string_len=count($string_arr);
        for($i=0;$i<$string_len;$i++)
        {
            if($string_arr[$i]==$word_item)
            {
                unset($string_arr[$i]);
                break;
            }
        }
        $tg_string=implode('|',$string_arr);
        $row=_query("update tg_system set tg_string='{$tg_string}'");
        //判断影响行数
        if(_affected_rows()>0)
        {
            $return_data['status']=1;
            $return_data['msg'] = '删除成功';
        }
        else
        {
            $return_data['status']=0;
            $return_data['msg']='请不要重复操作';
        }
    } //增加敏感词
    else if ($_POST['type'] == 'system_word_add') {
        //获取前台传递的值，并进行过滤
        $word_item = _mysql_string($_POST['word_item']);
        //生成sql语句，查找敏感词内容
        $sql1 = "select tg_string from tg_system limit 1";
        //执行sql语句
        $res = _fetch_array($sql1);
        //将新添加的值追加到原敏感词串之后
        $tg_string = $res['tg_string'] . '|' . $word_item;
        //执行修改
        $row = _query("update tg_system set tg_string='{$tg_string}'");
        //判断影响行数
        if (_affected_rows() > 0) {
            $return_data['status'] = 1;
            $return_data['msg'] = '增加成功';
        } else {
            $return_data['status'] = 0;
            $return_data['msg'] = '请不要重复操作';
        }
    }
    else
    {
        $return_data['msg']='请不要误操作';
    }
} else
{
    $return_data['msg']='请不要非法操作';
}
echo json_encode($return_data);